<?php

require_once __DIR__ . '/record.php';

class Produk extends record {

    protected $kategori;


    function __construct() {
        parent::__construct();
        $this->namaTabel = 'produk';
        $this->kategori = new kategori();
    }
    function getMinimStok(){
        $this->namaTabel="v_produk";
        $this->get("*",array(),10,null, 'jumlah','asc');
        $e = $this->execute();
        return isset($e['result'])?$e['result']:array();
    }
    function countBased($based,$id){
        $this->namaTabel="v_produk";
        $this->num_rows(array($based=>$id));
        $dt=  $this->execute();
        return ($dt['success']==true)?$dt['result'][0]['total']:0;
    }
    function getForEdit($id) {
        $this->get('*', array('id' => $id), 1);
        $res = $this->execute();
        if ($res['success'] == true && $res['total'] > 0) {
            $ret = $res['result'];
        } else {
            $ret = array();
        }
        return $ret;
    }
    function getComboMProduk(){
        $strC = '';
        $this->namaTabel='v_produk';
        $this->get('id,nama',array('aktif' =>1 ));
        $dt= $this->execute();
        if($dt['success']==false || ($dt['success']==true && $dt['total']==0)){
            $strC='<option>(tidak ada data)</option>';
        }
        else{
            $reS = $dt['result'];
            foreach ($reS as $vData) {
                $strC .= '<option value="'.$vData['id'].'">'.$vData['nama'].'</option>';
            }
        };
        return $strC;
    }
    function getCombo() {
        $str = '';
        $strQ = "SELECT p.id,p.nama,k.nama AS nama_kategori FROM produk p
LEFT JOIN kategori k
ON k.id = p.idkategori
WHERE p.aktif='1'";
        $this->query($strQ);
        $dt = $this->execute();
        if ($dt['success'] == true && $dt['total'] > 0) {
            $str .="<option>(pilih produk)</option>";
            foreach ($dt['result'] as $kaT) {
                $str .="<option value='" . $kaT['id'] . "'>" . $kaT['nama'] . "&nbsp;[" . $kaT['nama_kategori'] . "] </option>";
            }
        } else {
            $str .= "<option>(data kosong)</option>";
        };
        return $str;
    }

    function getData($limit = null, $start = null) {
        $this->namaTabel = 'v_produk';
        $this->num_rows();
        $total = $this->execute();
        $jml = ($total['success'] == true) ? $total['result'][0]['total'] : 0;
        $this->get('*', array(), $limit, $start,'waktu','DESC');
        $data = $this->execute();
        $dtS = ($data['success'] == TRUE) ? $data['result'] : array();
        $return = array('total' => $jml, 'result' => $dtS);
        return $return;
    }
    function getDisplayProduk($limit = null, $start = null,$orderBy,$typeOrd='ASC',$cond=array(),$strSearch='') {
        $this->namaTabel = 'v_produk';
$condS = array();
        if(strlen($strSearch)>0){
            $condS['nama']=$strSearch;
            $condS['nama_kategori']=$strSearch;
            $condS['harga_jual']=$strSearch;
            $condS['keterangan']=$strSearch;
        };
        $this->num_rows($cond,$condS);
        //return $this->strQuery;
        $total = $this->execute();
        $jml = ($total['success'] == true) ? $total['result'][0]['total'] : 0;
        $cond['aktif']=1;
        $condStr = ' jumlah > 0 ';
        $this->get('nama,keterangan,diskon,nama_kategori,gambar,id,harga_jual',$cond, $limit, $start, $orderBy, $typeOrd, $condStr,$condS);
        $data = $this->execute();
        $dtS = ($data['success'] == TRUE) ? $data['result'] : array();
        $return = array('total' => $jml, 'result' => $dtS);
        return $return;
    }
    function getDetail($id){
        $strQuery = "SELECT 
        * FROM v_produk p WHERE p.aktif='1' AND p.id='".$id."'";        
        $this->query($strQuery);
        $res = $this->execute();
        if ($res['success'] == true && $res['total'] > 0) {
            $ret = $res['result'][0];
        } else {
            $ret =false;
        }
        return $ret;
    }

}

  $objProduk = new Produk();